/*
 *  Copyright 2008-2010 the original author or authors.
 * 
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 * 
 *       http://www.apache.org/licenses/LICENSE-2.0
 * 
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  under the License.
 */
package org.openaecfm.spf.io.structuralload;

import org.openaecfm.model.structuralload.StructuralLoad;
import org.openaecfm.model.structuralload.StructuralLoadSingleForce;
import org.openaecfm.spf.io.AbstractEntityInstanceInputImpl;
import org.openaecfm.spf.io.IAppDataManager;
import org.openaecfm.spf.io.IEntityInstanceOutput;
import org.openaecfm.spf.io.IEntityInstanceUtilInput;
import org.openaecfm.spf.io.IEntityInstanceUtilOutput;
import org.openaecfm.spf.model.DollarParameter;
import org.openaecfm.spf.model.EntityInstance;
import org.openaecfm.spf.model.NodeType;
import org.openaecfm.spf.model.Parameter;
import org.openaecfm.spf.model.RealParameter;
import org.openaecfm.spf.model.SimpleEntityInstance;

public final class StructuralLoadSingleForceIO extends
		AbstractEntityInstanceInputImpl<StructuralLoadSingleForce> implements
		IEntityInstanceUtilInput<StructuralLoadSingleForce>,
		IEntityInstanceOutput<StructuralLoadSingleForce>,
		IEntityInstanceUtilOutput<StructuralLoadSingleForce> {
	private IEntityInstanceUtilInput<StructuralLoad> utilInput;
	private IEntityInstanceUtilOutput<StructuralLoad> utilOutput;

	public void setUtilOutput(
			IEntityInstanceUtilOutput<StructuralLoad> utilOutput) {
		this.utilOutput = utilOutput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public String exportSPF(StructuralLoadSingleForce pojo,
			IAppDataManager appDataManager) {
		SimpleEntityInstance ei = new SimpleEntityInstance(
				"IFCSTRUCTURALLOADSINGLEFORCE", appDataManager.getName(pojo));

		exportSPF(pojo, ei, appDataManager);

		return ei.toString();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	protected StructuralLoadSingleForce doCreateObject() {
		return new StructuralLoadSingleForce();
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void exportSPF(StructuralLoadSingleForce pojo, EntityInstance ei,
			IAppDataManager appDataManager) {
		utilOutput.exportSPF(pojo, ei, appDataManager);
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set ForceX
		if (pojo.getForceX() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo.getForceX()));
		}

		// Set ForceY
		if (pojo.getForceY() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo.getForceY()));
		}

		// Set ForceZ
		if (pojo.getForceZ() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo.getForceZ()));
		}

		// Set MomentX
		if (pojo.getMomentX() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo.getMomentX()));
		}

		// Set MomentY
		if (pojo.getMomentY() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo.getMomentY()));
		}

		// Set MomentZ
		if (pojo.getMomentZ() == null) {
			sei.addParameter(DollarParameter.getInstance());
		} else {
			sei.addParameter(new RealParameter(pojo.getMomentZ()));
		}
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveNotReferenceParameter(EntityInstance ei, StructuralLoadSingleForce pojo) {
		utilInput.resolveNotReferenceParameter(ei, pojo);
		SimpleEntityInstance sei = (SimpleEntityInstance) ei;

		// Set ForceX
		Parameter forcex = sei.getParameter(1);
		if (forcex.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) forcex;
			pojo.setForceX(r.toDouble());
		}

		// Set ForceY
		Parameter forcey = sei.getParameter(2);
		if (forcey.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) forcey;
			pojo.setForceY(r.toDouble());
		}

		// Set ForceZ
		Parameter forcez = sei.getParameter(3);
		if (forcez.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) forcez;
			pojo.setForceZ(r.toDouble());
		}

		// Set MomentX
		Parameter momentx = sei.getParameter(4);
		if (momentx.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) momentx;
			pojo.setMomentX(r.toDouble());
		}

		// Set MomentY
		Parameter momenty = sei.getParameter(5);
		if (momenty.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) momenty;
			pojo.setMomentY(r.toDouble());
		}

		// Set MomentZ
		Parameter momentz = sei.getParameter(6);
		if (momentz.getNodeType() == NodeType.REAL) {
			RealParameter r = (RealParameter) momentz;
			pojo.setMomentZ(r.toDouble());
		}
	}

	public void setUtilInput(IEntityInstanceUtilInput<StructuralLoad> utilInput) {
		this.utilInput = utilInput;
	}

	/**
	 * {@inheritDoc}
	 */
	@Override
	public void resolveReferenceParameter(EntityInstance ei,
			StructuralLoadSingleForce pojo, IAppDataManager appDataManager) {
		return;
	}
}
